package hongcheng.code_generator.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import hongcheng.code_generator.entity.JdbcTypeInformation;
import hongcheng.code_generator.entity.config.CodeGeneratorConfig;
import hongcheng.code_generator.entity.db.TableColumnMetadata;
import hongcheng.code_generator.entity.db.TableMetadata;
import hongcheng.code_generator.exception.CustomException;
import hongcheng.code_generator.utils.DbType2JavaTypeUtils;

/**
 * 	读取MySql数据库表元数据类
 * */
public class DefaultMySqlDBMetadataReader extends AbstractDBMetadataReader{
	
	/**
	 * 	构造函数
	 * */
	public DefaultMySqlDBMetadataReader(CodeGeneratorConfig config) {
		super(config);
	};
	

	/**
	 * 	获取数据库元数据
	 * */
	@Override
	public TableMetadata getDBMetadata()  {
		TableMetadata tableMetadata = new TableMetadata();
		try {
			// 获取数据库管理类
			DBManager dbManager = new DBManager(getUrl(),getUser(),getPassword());
			// 连接
			dbManager.connection();
			Connection connection = dbManager.getConnection();
			// 获取数据库名
			String dbName = connection.getCatalog();
			// 获取指定表的注释
			String sql = "select table_comment from information_schema.tables where table_schema = '{}' AND table_name = '{}'";
			sql = StrUtil.format(sql, dbName,getTableName());
			ResultSet resultSet = dbManager.executeQuery(sql);
			while(resultSet.next()) {
				// 设置表注释
				tableMetadata.setTableComment(StrUtil.isBlank(resultSet.getString(1))?"":resultSet.getString(1));
			}
			// 设置表名
			tableMetadata.setTableName(getTableName());
			
			// 查询表字段
			sql = "SELECT column_name,COLUMN_TYPE,column_comment,IF(COLUMN_KEY='PRI','true','false') as 'isPri' FROM  information_schema.COLUMNS WHERE table_schema = '{}' AND table_name = '{}'";
			sql = StrUtil.format(sql, dbName,getTableName());
			ResultSet resultSet2 = dbManager.executeQuery(sql);
			
			List<TableColumnMetadata> tableColumnList = new ArrayList<TableColumnMetadata>();
			TableColumnMetadata tableColumn = null;
			
			while(resultSet2.next()) {
				JdbcTypeInformation jdbcTypeInformation = DbType2JavaTypeUtils.getJdbcTypeInformation(resultSet2.getString(2));
				tableColumn = new TableColumnMetadata();
				tableColumn.setColumnComment(resultSet2.getString(3));
				tableColumn.setColumnName(resultSet2.getString(1));
				tableColumn.setJavaClassName(jdbcTypeInformation.getJavaClassFullName());
				tableColumn.setJavaType(jdbcTypeInformation.getJavaClassSimpleName());
				tableColumn.setJdbcType(jdbcTypeInformation.getMybatisJdbcType());
				tableColumn.setIsPrimaryKey(resultSet2.getBoolean(4));
				tableColumnList.add(tableColumn);
				if(resultSet2.getBoolean(4) == true) {
					tableMetadata.setPrimaryKey(tableColumn);
				}
			}
			tableMetadata.setTableColumnList(tableColumnList);
			dbManager.close();
		} catch (Exception e) {
			CustomException exception = new CustomException("获取表元数据异常");
			exception.addSuppressed(e);
			throw exception;
		}
		return tableMetadata;
	}
	
}
